package netty;

import java.net.SocketAddress;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpServerCodec;
import org.jboss.netty.handler.codec.http.HttpVersion;

import com.google.common.base.Charsets;
import com.google.common.util.concurrent.AbstractIdleService;
import com.google.inject.Inject;
import com.google.inject.Provider;

class NettyServer extends AbstractIdleService {	
	private final ChannelGroup allChannels;
	private final SocketAddress address;
	private final ChannelFactory factory;
	private final ServerBootstrap bootstrap;
	private final Provider<Handler> handler;
	
	@Inject
	NettyServer(ChannelFactory factory, ChannelGroup allChannels, SocketAddress address, Provider<Handler> handler) {
		this.factory = factory;
		this.bootstrap = new ServerBootstrap(factory);
		this.allChannels = allChannels;
		this.address = address;
		this.handler = handler;
	}

	@Override
	protected void startUp() throws Exception {
		bootstrap.setPipelineFactory(new ChannelPipelineFactory() {			
			public ChannelPipeline getPipeline() throws Exception {
				return Channels.pipeline(new HttpServerCodec(), handler.get());
			}
		});		
		bootstrap.setOption("child.tcpNoDelay", true);
		bootstrap.setOption("child.keepAlive", true);
		Channel channel = bootstrap.bind(address);
		allChannels.add(channel);		
	}

	@Override
	protected void shutDown() throws Exception {
		allChannels.close().awaitUninterruptibly();
		factory.releaseExternalResources();
	}
}

class Handler extends SimpleChannelHandler {
	private final ChannelGroup allChannels;
	
	@Inject
	Handler(ChannelGroup allChannels) {
		this.allChannels = allChannels;
	}

	@Override
	public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {		
		allChannels.add(e.getChannel());
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
			throws Exception {		
		e.getCause().printStackTrace();
		e.getChannel().close();
	}

	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
			throws Exception {		
		HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
		response.setContent(ChannelBuffers.copiedBuffer("Hello, world!", Charsets.UTF_8));
		e.getChannel().write(response).addListener(new ChannelFutureListener() {			
			public void operationComplete(ChannelFuture future) throws Exception {
				future.getChannel().close();				
			}});
	}
	
}